/*
 * Application\Inc\flash_mem.h
 *
 *
 *
 * Author: Chao-Chi Kuo <chaochi_kuo@systech.com.tw>
 * Copyright (c) 2010 SYSTEMS & TECHNOLOGY CORP. <http://www.systech.com.tw/>
 * All rights reserved.
 *
 * Modifications:
 * 2009/12/10 Chao-Chi Kuo	first implemetation
 *
 * $Id: flash_mem.h 196 2010-08-05 01:54:48Z chaochi_kuo $
 */
#ifndef __FLASH_MEM_H_
#define __FLASH_MEM_H_

#ifdef  __cplusplus
extern "C" {
#endif		/* __cplusplus */


#define PAGE_SIZE			256				///< Flash Page Size: 256 Byte
#define SSEC_SIZE			(4 * 1024)		///< Flash SubSector Size: 4KB
#define SEC_SIZE			(64 * 1024)		///< Flash Sector Size: 64KB


#define FLASH_MEM_START		0					///< Flash Start Address
#define FLASH_MEM_SIZE		(8 * 1024 * 1024)	///< Flash Size: 64Mb = 8MB

#define TOTAL_SSEC			(FLASH_MEM_SIZE / SSEC_SIZE)	///< Total SubSector number: 2MB / 4KB = 512
#define TOTAL_SEC			(FLASH_MEM_SIZE / SEC_SIZE)		///< Total Sector number: 2MB / 64KB = 32

/** FLASH Memory Layout
 *
 * -----------------
 * | System        |	4K	Restore System Parameter(Normal Type)
 * -----------------
 * | Param         |	4K	Restore Default & User Parameter(Normal Type)
 * -----------------
 * | AGPS          |	8K	Restore AGPS Data(Normal Type)
 * -----------------
 * | Reserve       |	516K	Reserve
 * -----------------
 * | Log           |	1068K	Restore Log Data(QBuffer Type)
 * -----------------
 * | Priority gprs |	32K		Restore Priority Data sent by GPRS(QBuffer Type)
 * -----------------
 * | Priority sms  |	32K		Restore Priority Data sent by SMS(QBuffer Type)
 * -----------------
 * | gprs log      |	128K	Restore Log Data sent by GPRS(QBuffer Type)
 * -----------------
 * | sms log       |	128K	Restore SMS Data sent by SMS(QBuffer Type)
 * -----------------
 * | OTA           |	128K	Restore Firmware Data(Normal Type)
 * -----------------
 *
 * Normal Type	: Access by SPI_Flash_Write / SPI_Flash_Read ...
 * QBuffer Type	: Access by Flash_Queue_Add / Flash_Queue_Read ...
 */
/* Define block size (Sub Sectore) */
#define SYSTEM_SIZE			1	// 1 * 4K = 4K
#define PARAM_SIZE			2   // 2 * 4K = 8K
#define INIT_DATA_SIZE      2   // 2 * 4K = 8K
#define IMU_PARAM_SIZE		1	// 1 * 4K = 4K
#define AGPS_SIZE			2	// 2 * 4K = 8K
#define RESERVE_SIZE		129 // 129 * 4K = 516K
#define LOG_SIZE			267	// 267 * 4K = 1068K	// 45 * 267 = 12015
#define PRIORITY_GPRS_SIZE	8	// 8 * 4K = 32K
#define PRIORITY_SMS_SIZE	8	// 8 * 4K = 32K
#define GPRS_LOG_SIZE		32	// 32 * 4K = 128K
#define SMS_LOG_SIZE		32	// 32 * 4K = 128K
#define OTA_SIZE			32	// 32 * 4K = 128K

/* Define block Address */
#define SYSTEM_ADDR			(FLASH_MEM_START)
#define PARAM_ADDR			(SYSTEM_ADDR + SYSTEM_SIZE * 4 * 1024)
#define INIT_DATA_ADDR		(PARAM_ADDR + PARAM_SIZE * 4 * 1024)
#define IMU_PARAM_ADDR		(INIT_DATA_ADDR + INIT_DATA_SIZE * 4 * 1024)
#define AGPS_ADDR			(IMU_PARAM_ADDR + IMU_PARAM_SIZE * 4 * 1024)
#define RESERVE_ADDR		(AGPS_ADDR + AGPS_SIZE * 4 * 1024)
#define LOG_ADDR			(RESERVE_ADDR + RESERVE_SIZE * 4 * 1024)
#define PRIORITY_GPRS_ADDR	(LOG_ADDR + LOG_SIZE * 4 * 1024)
#define PRIORITY_SMS_ADDR	(PRIORITY_GPRS_ADDR + PRIORITY_GPRS_SIZE * 4 * 1024)
#define GPRS_LOG_ADDR		(PRIORITY_SMS_ADDR + PRIORITY_SMS_SIZE * 4 * 1024)
#define SMS_LOG_ADDR		(GPRS_LOG_ADDR + GPRS_LOG_SIZE * 4 * 1024)
#define OTA_ADDR			(FLASH_MEM_START + FLASH_MEM_SIZE - OTA_SIZE * 4 * 1024)


/* PARAM Layout
 * -----------------
 * | Default Table |
 * -----------------
 * | User Table    |
 * -----------------
 */
#define TBL_SIZE			(4 * 1024)	// 4K
#define DEFAULT_TBL_ADDR	(PARAM_ADDR)
#define USER_TBL_ADDR		(DEFAULT_TBL_ADDR + TBL_SIZE)

/* Define Flash QBuffer Index number */
#define LOG_FQBUF			0
#define PRIORITY_GPRS_FQBUF	1
#define PRIORITY_SMS_FQBUF	2
#define GPRS_LOG_FQBUF		3
#define SMS_LOG_FQBUF		4


/* Qbuffer Type Access Function */
extern void Flash_Mem_Init(void);
extern unsigned short Flash_Queue_Get_num(unsigned char id);
extern void Flash_Queue_Add(unsigned char id, char *buf, unsigned short len);
extern void Flash_Queue_Del(unsigned char id);
extern unsigned short Flash_Queue_Read(unsigned char id, char *buf, unsigned short num, unsigned short buf_len);
extern void FQBuf_Init(unsigned char id);


#ifdef __cplusplus
}
#endif	/* __cplusplus */
#endif	/* __FLASH_MEM_H_ */